// service统一出口
import RVRequest from './request';
import { BASE_URL, TIME_OUT } from './config';
import { storage } from '../utils';

const rvRequest = new RVRequest({
    baseURL: BASE_URL,
    timeout: TIME_OUT,
    interceptors: {
        requestInterceptor: (config: any) => {
            // 携带token的拦截
            const appId = storage.getSession('appId');
            const token = storage.getSession('iot-token');
            const appKey = storage.getSession('appKey');
            const baseHeaders: anyObject = {
                'Content-Type': 'application/json; charset=UTF-8',
                'iot-token': token,
                appId,
                appKey
            };
            config.headers = { ...baseHeaders };
            return config;
        },
        requestInterceptorCatch: err => {
            return err;
        },
        responseInterceptor: res => {
            const token = res?.headers['iot-token'];
            if (token) {
                storage.setSession('iot-token', token);
            }
            return res;
        },
        responseInterceptorCatch: err => {
            return err;
        }
    }
});

export default rvRequest;
